﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;

public class Panel_SignIn : BasePanel
{
    CanvasGroup canvas;
    /// <summary>
    /// 签到图标列表
    /// </summary>
    public List<GameObject> signList;
    private Transform todaySign;

    private void Awake()
    {
        canvas = transform.GetComponent<CanvasGroup>();
    }

    private void Start()
    {
        StatusOfSign();
    }


    public override void OnEnter()
    {
        base.OnEnter();
        gameObject.SetActive(true);

    }

    public override void OnExit()
    {
        base.OnExit();
        gameObject.SetActive(false);

    }

    public override void OnPause()
    {
        base.OnPause();
        canvas.alpha = 0;
        canvas.blocksRaycasts = false;
    }

    public override void OnResume()
    {
        base.OnResume();
        canvas.alpha = 1;
        canvas.blocksRaycasts = true;
    }

    public void ClosePanel()
    {
        UIManager.Instance.PopPanel();
    }

    private void Update()
    {
        if (Input.GetButtonDown("Cancel"))
        {
            GameControl.instance.GameOver();
            UIManager.Instance.PopPanel();
        }
    }

    /// <summary>
    /// 签到按钮函数执行
    /// </summary>
    public void Button_Sign()
    {
        //Debug.Log("DateTime.Today" + DateTime.Today);
        //Debug.Log("DateTime.Now" + DateTime.Now);
        //Debug.Log("DateTime.UtcNow" + DateTime.UtcNow);
        //Debug.Log("DateTime.Now.Month" + DateTime.Now.Month);
        //Debug.Log("DateTime.Now.Day" + DateTime.Now.Day);
        //Debug.Log("DateTime.Now.Year" + DateTime.Now.Year);
        //Debug.Log("DateTime.Now.DayOfWeek" + DateTime.Now.DayOfWeek);
        //Debug.Log("DateTime.Now.DayOfYear" + DateTime.Now.DayOfYear);
        
        if (User.instance.CanSign)
        {
            /*******************/
            ////广告位
            /*******************/

            User.instance.HaveSign = true;
            User.instance.CanSign = false;
            User.instance.DaysOfSign += 1;
            PlayerPrefs.SetInt("DateOfSign", User.instance.DateOfToday);
            PlayerPrefs.SetInt("DaysOfSign", User.instance.DaysOfSign);
            //Debug.Log("knock");
            int moneyNum = int.Parse(todaySign.Find("MoneyLogo").Find("MoneyNum").GetComponent<Text>().text);
            User.instance.ChangeMoney(moneyNum);
            ChangeForSign(todaySign);
        }

    }

    public void BoolForSign()
    {
        //dateOfYear = DateTime.Now.DayOfYear;
        //dateOfSign = PlayerPrefs.GetInt("dayOfSign", 0);
        //if (dateOfSign != dateOfYear)
        //{
        //    canSign = true;
        //}
        //else
        //{
        //    canSign = false;
        //}

    }

    /// <summary>
    /// 更新签到状态
    /// </summary>
    public void StatusOfSign()
    {
        if (User.instance.DaysOfSign == 7)
        {
            User.instance.DaysOfSign = 0;
        }
        //Debug.Log("daysOfSign ======= " + User.instance.DaysOfSign);
        //Debug.Log("dateOfSign ======= " + User.instance.DateOfSign);
        //Debug.Log("dateOfYear ======= " + User.instance.DateOfToday);

        for (int i = 0; i < signList.Count; i++)
        {
            //已签到
            if (i < User.instance.DaysOfSign)
            {
                ChangeForSign(signList[i].transform);
            }
            //未签到
            else
            {
                if (i == User.instance.DaysOfSign && !User.instance.HaveSign)
                {
                    todaySign = signList[i].transform;
                    Transform obj = signList[i].transform.Find("Today");
                    if (obj != null)
                    {
                        obj.GetComponent<Image>().enabled = true;
                    }
                    //Debug.Log(todaySign.name);
                    break;
                }
            }
        }
    }


    /// <summary>
    /// 签到，更改对应图片
    /// </summary>
    /// <param name="signItem"></param>
    public void ChangeForSign(Transform signItem)
    {
        Transform obj = signItem.Find("HaveSign");
        if (obj != null)
        {
            obj.GetComponent<Image>().enabled = true;
            //Debug.Log("haveSign");
        }

        Transform obj1 = signItem.Find("Text");
        if (obj1 != null)
        {
            obj1.GetComponent<Text>().enabled = false;
            //Debug.Log("Text");
        }

        Transform obj2 = signItem.Find("SignShadow");
        if (obj2 != null)
        {
            obj2.GetComponent<Image>().enabled = true;
            //Debug.Log("SignShadow");
        }
    }



}
